import Taro, { Component, Config } from '@tarojs/taro';
import { View, Text, Image } from '@tarojs/components';
import './index.less';
import pImg from '../../images/4-4.png';
import pImg1 from '../../images/5-2.png';
import { addNum, initData } from '../../utils/answer-helper';
let globalData = Taro.getApp().globalData;
export default class Index extends Component {
  constructor(props) {
    super(props);
    this.state = {
      showHide: false,
      score: 0, //分数
      scoreTips: '', //分数提示，
      rightAnswer: [2, 7, 12, 13, 18], //正确答案
      scoreTipsArr: [
        '你说，是不是把知识都还给小学老师了？',
        '还不错，但还需要继续加油哦！',
        '不要嘚瑟还有进步的空间！',
        '智商离爆表只差一步了！',
        '你也太聪明啦，葡萄之家欢迎你！'
      ]
    };
  }
  componentWillMount() {}

  componentDidMount() {}

  componentWillUnmount() {}

  componentDidShow() {
    this.setState(
      {
        globalData: globalData
      },
      () => {
        this.computedScore();
      }
    );
  }

  componentDidHide() {
  }
  //计算分数
  computedScore() {
    let { score, globalData, rightAnswer } = this.state;
    globalData.answerid.forEach((item, index) => {
      if (item == rightAnswer[index]) {
        score += 20;
      }
    });
    this.setState({ // 异步
      score: score
    },()=>{
      setTimeout(()=>{ // 设置延迟，解决h5数据获取延迟问题  setstate是异步的
        this.getScoretip()
      },0)
    });
  }
  //提示信息
  getScoretip() {
    let {score,scoreTipsArr,scoreTips} = this.state
    console.log(score)
    let index = Math.ceil(score / 20) - 1;
    console.log(index)
    scoreTips = scoreTipsArr[index];
    console.log(scoreTips)
    this.setState({
      scoreTips:scoreTips
    })
  }
  render() {
    const show = false;
    return (
      <View className="index">
        <View className="home_container">
          <View>
            <View className="your_scores_container">
              <View className="your_scores">
                <Text className="score_num">{this.state.score}</Text>
                <Text className="fenshu">分！</Text>
              </View>
              <View className="result_tip">
                {this.state.scoreTips}
              </View>
            </View>
            <View className="share_button" />
            <View className="share_code">
              <View className="share_header">关注葡萄之家，获取答案</View>
              <Image src={pImg} className="code_img" />
            </View>
            {show ? (
              <View className="share_cover">
                <Image src={pImg1} className="share_Image" />
              </View>
            ) : (
              ''
            )}
          </View>
        </View>
      </View>
    );
  }
}
